﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KongZhiTaiTest.LanguageBase
{
    public class UsingAndTryCatch
    {

        public static void ShowUsingTest()
        {
            UsingTest();
        }

        public static void ShowTryCatch()
        {
            TryCatchTest();
        }

        public static void ShowUsingAndTryCatch()
        {
            UsingAndTryCatchTest();
        }

        public static void ShowUsingAndTryCatch2()
        {
            UsingAndTryCatchTest2();
        }

        private static void UsingTest()
        {
            string str = "I Do not know What it is?";
            string path = AppDomain.CurrentDomain.BaseDirectory + "test.txt";
            using (var fileStream = new FileStream(path, FileMode.OpenOrCreate))
            {
                Console.WriteLine($"输出的路径为：{path}");
                Task task = fileStream.WriteAsync(Encoding.Default.GetBytes(str), 0, str.Length);
                task.Wait();
            }
        }

        /// <summary>
        /// 测试TryCatch
        /// </summary>
        private static void TryCatchTest()
        {
            string str = "I Do not know What it is?";
            string path = AppDomain.CurrentDomain.BaseDirectory + "test.txt";
            FileStream fileStream = default;
            try
            {
                fileStream = new FileStream(path, FileMode.OpenOrCreate);
                Console.WriteLine($"输出的路径为：{path}");
                Task task = fileStream.WriteAsync(Encoding.Default.GetBytes(str), 0, str.Length);
                task.Wait();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                fileStream.Dispose();
            }
        }

        /// <summary>
        /// 联合应用：想用catch 捕获异常又想用Using自动释放资源
        /// </summary>
        private static void UsingAndTryCatchTest()
        {
            string str = "I Do not know What it is?";
            string path = AppDomain.CurrentDomain.BaseDirectory + "test.txt";
            FileStream fileStream = default;
            try
            {
                using (fileStream = new FileStream(path, FileMode.OpenOrCreate))
                {
                    Console.WriteLine($"输出的路径为：{path}");
                    Task task = fileStream.WriteAsync(Encoding.Default.GetBytes(str), 0, str.Length);

                    task.Wait();
                    throw new Exception("故意出错！");
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("有异常，有异常，我捕获到了！！！");
            }
            finally
            {
                Console.WriteLine("使用catch捕获异常但是使用using自动释放资源");
                //这句话就重复了因为已经关取消了。
                fileStream.Close();
            }
        }

        /// <summary>
        /// 联合应用：想用catch 捕获异常又想用Using自动释放资源
        /// </summary>
        private static void UsingAndTryCatchTest2()
        {
            string str = "I Do not know What it is?";
            string path = AppDomain.CurrentDomain.BaseDirectory + "test.txt";
            FileStream fileStream = default;
            using (fileStream = new FileStream(path, FileMode.OpenOrCreate))
            {
                try
                {

                    Console.WriteLine($"输出的路径为：{path}");
                    Task task = fileStream.WriteAsync(Encoding.Default.GetBytes(str), 0, str.Length);

                    task.Wait();
                    throw new Exception("故意出错！");


                }
                catch (Exception ex)
                {
                    Console.WriteLine("有异常，有异常，我捕获到了！！！");
                }
                finally
                {
                    Console.WriteLine("使用catch捕获异常但是使用using自动释放资源");
                    //这句话就重复了因为已经关取消了。
                    fileStream.Close();
                }
            }
        }
    }
}
